package server;

import java.io.*;
import java.net.*;

public class TestingServer {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		ServerSocket serverSocket = null;
		int port = 23;
		try {
			serverSocket = new ServerSocket(port);
		    System.out.println("Server socket open on port "+port);
		} catch (IOException e) {
			System.err.println("Could not listen on port: "+port);
		    System.exit(1);
		}
		
		Socket clientSocket = null;
		try {
			clientSocket = serverSocket.accept();
		    System.out.println("Client socket opened.");
		} catch (IOException e) {
			System.err.println("Accept failed.");
			System.exit(1);
		}
		
		PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
		BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
		
		String inputLine;
		
		InetAddress add = clientSocket.getInetAddress();
		out.println("You are now connected to "+add.getHostName()+" ("+add.getHostAddress()+") on port "+port);
		
		while ((inputLine = in.readLine()) != null) {
		    System.out.println("Client said: "+inputLine);
			out.println("You said: "+inputLine);
			if (inputLine.equals("exit")) {
				System.out.println("Shutting down...");
				break;
			}
				
		}
		
		out.close();
		in.close();
		clientSocket.close();
		serverSocket.close();
    }
}
